﻿using System.IO;

using FirstFloor.Documents.Controls.Commands;
using FirstFloor.Documents.IO;
using FirstFloor.Documents.Pdf;

namespace FirstFloor.Documents.Presentation.Commands
{
    /// <summary>
    /// The command that opens a PDF or XPS document from disk and assigns it to a DocumentDataSource.
    /// </summary>
    /// <remarks>
    /// XPS documents are loaded using the default package reader.
    /// </remarks>
    public class OpenPdfOrXpsFromDiskCommand
        : OpenFromDiskCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenPdfOrXpsFromDiskCommand"/> class.
        /// </summary>
        public OpenPdfOrXpsFromDiskCommand()
        {
            this.Filter = "Documents (*.pdf,*.xps,*.dwfx)|*.pdf;*.xps;*.dwfx|PDF Documents (*.pdf)|*.pdf|XPS Documents (*.xps,*.dwfx)|*.xps;*.dwfx|All Files (*.*)|*.*";
        }

        /// <summary>
        /// Creates a package reader for given stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public override IPackageReader CreatePackageReader(Stream stream)
        {
            if (PdfDocumentReader.IsPdfStream(stream)) {
                return new PdfDocumentReader(stream);
            }
            else {
                // reset stream pos
                stream.Seek(0, SeekOrigin.Begin);
                return new DefaultPackageReader(stream);
            }
        }
    }
}
